<template>
  <div>
    <Row class="search-info-style">
      <Col span="6">
        <span>&nbsp;届&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;次&nbsp;：</span>
        <Input v-model="searchInfo.year" placeholder="届次" style="width:70%" />
      </Col>
      <Col style="float:right" pull="1">
        <Button class="new-btn-sty" @click="findtime()"><Icon type="ios-search" />搜索</Button>
        <Button class="new-btn-sty" @click="add()"><Icon type="md-add" />添加</Button>
      </Col>
    </Row>
    <Table border :columns="columns1" :data="data1"></Table>
    <Row v-if="loadData == true">
      <Col class="demo-spin-col" span="8" offset="8">
        <Spin fix>
          <Icon type="ios-loading" size=18 class="demo-spin-icon-load"></Icon>
          <div>数据正在加载</div>
        </Spin>
      </Col>
    </Row>
    <Page class="page_style" :total="totalnum" :current="currentPageNo"
          :page-size="currentPageSize" @on-change="changePageNo" show-total  show-sizer @on-page-size-change="size"/>
    <Modal v-model="modalEdit" title="届次信息" @on-ok="OK111">
      <Form :model="paramData" >
        <Row class="item1">
          <Col span="5" style="text-align: right" class="">届次:</Col>
          <Col span="12" offset="1">
            <Input v-model="paramData.year" placeholder="请输入届次"/>
          </Col>
        </Row>
        <Row class="item1">
          <Col span="5" style="text-align: right" class="">类型:</Col>
          <Col span="12" offset="1">
            <RadioGroup v-model="paramData.type" type="button" button-style="solid">
              <Radio label="1" border>农村</Radio>
              <Radio label="2" border>社区</Radio>
            </RadioGroup>
          </Col>
        </Row>
        <Row class="item1">
          <Col span="5" style="text-align: right" class="">届次开始时间:</Col>
          <Col span="12" offset="1">
            <DatePicker type="date" placeholder="选择日期" v-model="paramData.start_time"></DatePicker>
          </Col>
        </Row>
        <Row  class="item1">
          <Col span="5" style="text-align: right" class="">届次结束时间:</Col>
          <Col span="12" offset="1">
            <DatePicker type="date" placeholder="选择日期" v-model="paramData.end_time"></DatePicker>
          </Col>
        </Row>
      </Form>
    </Modal>
  </div>
</template>
<script>
import { SessionList, date2str, saveSession } from '@/api/eventlist'
export default {
  data () {
    return {
      currentPageNo: 1,
      currentPageSize: 10,
      totalnum: 0,
      loadData: true,
      modalEdit: false,
      searchInfo: {},
      paramData: {},
      param: {},
      data1: [],
      columns1: [
        { title: '序号', type: 'index', width: 80, align: 'center' },
        { title: '届次', key: 'year', width: 200, align: 'center' },
        { title: '开始时间', key: 'start_time', width: 200, align: 'center' },
        { title: '结束时间', key: 'end_time', width: 200, align: 'center' },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          className: 'nopadding',
          minWidth: 200,
          render: (h, params) => {
            let temp = []
            temp.push(
              h('Button', {
                props: { type: 'error', size: 'small' },
                style: { marginRight: '5px', backgroundColor: '#18B0FF', borderColor: '#18B0FF' },
                on: {
                  click: () => {
                    this.downLoad(params.row)
                  }
                }
              }, '编辑')
            )
            return h('div', temp)
          }
        }
      ]
    }
  },
  methods:{
    init() {
      let param = {
        pageSize: this.currentPageSize,
        pageNo: this.currentPageNo,
        year: this.searchInfo.year
      }
      SessionList(param).then(res => {
        if (res.data.errcode === 0) {
          this.data1 = res.data.data.results
          this.totalnum = res.data.totalRecord
          this.loadData = false
        } else {
          this.loadData = false
          this.$Notice.error({ title: '获取列表失败', desc: res.data.errmsg })
        }
      })
    },
    findtime() {
      this.init()
    },
    downLoad(item) {
      this.modalEdit = true
      let param = this.$common.xeUtils.clone(item,true)
      this.paramData = param
    },
    add() {
      this.paramData = {}
      this.modalEdit = true
    },
    OK111() {
      this.paramData.start_time = date2str(this.paramData.start_time)
      this.paramData.end_time = date2str(this.paramData.end_time)
      saveSession(this.paramData).then(res => {
        if (res.data.errcode === 0) {
          this.$Notice.success({ title: '保存成功', desc: '' })
          this.init()
        } else {
          this.$Notice.error({ title: '保存失败', desc: res.data.errmsg })
          this.init()
        }
      })
    },
    size (pageSize) {
      this.currentPageSize = pageSize
      this.changePageNo()
    },
    changePageNo (pageNo) {
      this.currentPageNo = pageNo
      this.init()
    }
  },
  mounted() {
    this.init()
  }
}
</script>
<style>
  .search-info-style {
    margin: 10px;
  }
  .new-btn-sty {
    margin-right: 5px;
    color: #fff;
    background-color: #235CA7;
    border-color: #235CA7;
    font-weight: 500;
  }
  .item1{
    height: 42px;
    line-height: 40px;
  }
  .page_style {
    margin-top: 10px;
    text-align: right;
  }
</style>
